package com.lzc.camera.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzc.camera.common.CommonPage;
import com.lzc.camera.common.CommonResult;
import com.lzc.camera.entity.Config;
import com.lzc.camera.service.ConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzc
 * @since 2022-11-20 08:54:54
 */
@CrossOrigin
@RestController
@RequestMapping("/config")
public class ConfigController {

    @Autowired
    private ConfigService configService;

    @ApiOperation(value = "配置增加页面")
    @RequestMapping("add")
    @ResponseBody
    public CommonResult addConfig(@RequestBody Config config){
        QueryWrapper<Config> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("point_key",config.getPointKey());
        long count = configService.count(queryWrapper);
        if (count>0){
            return CommonResult.failed("点位key重复，请修改后再试！");
        }
        boolean save = configService.save(config);
        if (save){
            return CommonResult.success(config.getId());
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "配置修改页面")
    @RequestMapping("update/{id}")
    @ResponseBody
    public CommonResult updateConfig(@PathVariable Integer id,@RequestBody @Validated Config config){
        boolean update = configService.updateById(config);
        if (update){
            return CommonResult.success(config.getId());
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "配置删除页面")
    @RequestMapping("delete/{id}")
    @ResponseBody
    public CommonResult deleteConfig(@PathVariable Integer id){
        boolean remove = configService.removeById(id);
        if (remove){
            return CommonResult.success(id);
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "配置查询页面")
    @RequestMapping("select")
    @ResponseBody
    public CommonResult selectConfig(Config config, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        List<Config> list = configService.list(config, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation(value = "根据图片唯一标识返回图片地址")
    @RequestMapping("getimage")
    @ResponseBody
    public CommonResult getImage(String imageName){
        //拼接图片地址
        String url = "http://127.0.0.1/img/"+imageName+".jpg";
        return CommonResult.success(url);
    }

}
